using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Sixeyed.CacheAdapter.Helpers
{
    /// <summary>
    /// Helper class for accessing streams
    /// </summary>
    public static class StreamHelper
    {
        /// <summary>
        /// Returns a byte array of the given stream contents
        /// </summary>
        /// <param name="requestStream">Stream</param>
        /// <returns>Stream contents</returns>
        public static byte[] GetBytes(Stream inputStream)
        {
            long initialSize = inputStream.Length > 0 ? inputStream.Length : 32768;
            byte[] request = new byte[initialSize];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = inputStream.Read(request, 0, request.Length);
                    if (read <= 0)
                    {
                        break;
                    }
                    ms.Write(request, 0, read);
                }
            }
            return request;
        }
    }
}
